<?php
namespace Admin\Controller;
use Think\PageAdmin;
use Common\Support\Curl;
use Think\Upload;

/**
 * 广告管理控制器
 */
class AdsController extends BaseController
{
	/**
     * 初始化
     * 
     * @return [type] [description]
     */
    public function _initialize()
    {
        parent::_initialize();       
    }

   	/**
   	 * 广告信息
   	 * @return [type] [description]
   	 */
    public function index(){
        $prefix = C('DB_PREFIX');

        $count = M('ads')->count();
        $page  = new PageAdmin($count, C('PAGE_NUM'));

        $pages['show']  = $page->show();  //分页输出
        $pages['total'] = $count;  //总数
        $pages['lists'] = M('ads a')
            ->order("a.id desc")
            ->limit($page->firstRow.','.$page->listRows)
            ->select();

        $this->seotitle = '广告列表';
        $this->pages    = $pages;
        $this->display();
    }

    public function add(){
        $this->seotitle = '添加广告位';
        $this->display();
    }

    /**
     * 广告位保存
     * @return [type] [description]
     */
    public function do_add()
    {
        $action = I('get.action', '', 'trim');
        $params = I('post.');

        if($params['ads_name'] == ''){
            $this->ajaxReturn(['status' => false, 'code' => 1001,'msg' => '广告名不能为空']);
        }

        if($params['ads_cc'] == ''){
            $this->ajaxReturn(['status' => false, 'code' => 1001,'msg' => '广告名尺寸不能为空']);
        }

        $ads_detail = htmlspecialchars_decode($params['data']);

        //echo $ads_detail = '[{"X":"22","Y":"33","W":"44","H":"55","T":1}]';

        $ads_detail = json_decode($ads_detail,true);

        if(empty($ads_detail)){
            $this->ajaxReturn(['status' => false, 'code' => 1001,'msg' => '请输入广告位具体参数']);
        }

        $info = M('ads')->where(['ad_name'=>$params['ads_name']])->find();
        if($info){
            $this->ajaxReturn(['status' => false, 'code' => 1001,'msg' => '请勿重新添加']);
        }


        $r = M('ads')->add([
                'ad_name'           => trim($params['ads_name']),
                'height'            => $params['ads_cc'],
                'width'             => $params['ads_cc'] ,
                'filepath'          => $params['filepath'] ,
                'is_upper_air'      => $params['is_upper_air'] == true ? 1 : 0,
                'admin_id'          => 1,
                'add_time'          => time(),
                'update_time'       => time()
            ]);

        if($r){

            foreach ($ads_detail as $k => $v) {
                if($v['T'] == 1){
                    //矩形
                    $arr = [
                                'ads_id'    => $r,
                                'ads_name'  => $v['JU_NAME'],
                                'type'      => $v['T'],
                                'x_axis'    => $v['X'], 
                                'y_axis'    => $v['Y'], 
                                'width'     => $v['W'], 
                                'height'    => $v['H'], 
                                'add_time'          => time(),
                                'update_time'       => time()                       
                            ];
                    
                }else{
                    //圆形
                    $arr = [
                                'ads_id'    => $r,
                                'ads_name'  => $v['YUAN_NAME'],
                                'type'      => $v['T'],
                                'x_axis'    => $v['X'], 
                                'y_axis'    => $v['Y'], 
                                'radius'    => $v['R'], 
                                'add_time'          => time(),
                                'update_time'       => time()                       
                            ];
                }

                M('ads_detail')->add($arr);
            }

            $this->ajaxReturn(['status' => true, 'code' => 1000,'msg' => '广告位添加成功']);

        }else{
            $this->ajaxReturn(['status' => false, 'code' => 1001,'msg' => '系统异常']);
        }
       
    }

    //绘图
    public function gd_tu(){

        $action = I('post.action', '', 'trim');
        $params = I('post.');

        if($params['ads_name'] == ''){
            $this->ajaxReturn(['status' => false, 'code' => 1001,'msg' => '广告名不能为空']);
        }

        if($params['ads_cc'] == ''){
            $this->ajaxReturn(['status' => false, 'code' => 1001,'msg' => '广告名尺寸不能为空']);
        }

        $ads_detail = htmlspecialchars_decode($params['data']);

        //echo $ads_detail = '[{"X":"22","Y":"33","W":"44","H":"55","T":1}]';

        $ads_detail = json_decode($ads_detail,true);

        if(empty($ads_detail)){
            $this->ajaxReturn(['status' => false, 'code' => 1001,'msg' => '请输入广告位具体参数']);
        }


        $width = $params['ads_cc'];                                              //图像宽度
        $height = $params['ads_cc'];                                             //图像高度
        $img = imagecreatetruecolor($width,$height);               //创建图像
        $white = imagecolorallocate($img,255,255,255);             //白色
        $black = imagecolorallocate($img,0,0,0);                   //黑色
        $grey = imagecolorallocate($img,220,220,220);              //灰色
        $blue = imagecolorallocate($img,238,44,44);              //填充色
        imagefill($img,0,0,$grey);                                  //将背景设置为灰色
        
        foreach ($ads_detail as $k => $v) {

            if($k%3 == 0){
                $blue = imagecolorallocate($img,255,153,102);
            }elseif($k%3 == 1){
                $blue = imagecolorallocate($img,255,102,102);
            }elseif($k%3 == 2){
                $blue = imagecolorallocate($img,255,204,204);
            }
            $font = 'Public/font/tt.OTF';

            if($v['T'] == 1){
                //矩形
                
                imagefilledrectangle($img,$v['X'],$v['Y'],$v['X']+$v['W'],$v['Y']+$v['H'],$blue);
                imagettftext($img, 12, 0, $v['X']+20, $v['Y']+20, $black, $font, $v['JU_NAME']);
                imagettftext($img, 12, 0, $v['X']+20, $v['Y']+60, $black, $font, $v['W'].'*'.$v['H']);
                
            }else{
                //圆形
                imagefilledellipse($img,$v['X'],$v['Y'],$v['R'],$v['R'],$blue);
                imagettftext($img, 12, 0, $v['X']+20, $v['Y']+20, $black, $font, $v['YUAN_NAME']);
                imagettftext($img, 12, 0, $v['X']+20, $v['Y']+40, $black, $font, $v['R']);
            }
        }
        

        header("content-type: image/png");                    //输出图像的MIME类型
        //imagepng($img);                                          //输出一个PNG图像数据

        $path = "Uploads/gd/".date("Y-m-d");
        if(!is_dir($path)){
             mkdir($path,2770,true);
        }

        $file = $path."/".rand(11111111,99999999).".png";
        

        imagepng($img,$file);

        imagedestroy($img);                                      //清空内存

        $this->ajaxReturn(['status' => true, 'code' => 1000,'msg' => '预览图生成成功','res'=>$file]);
    }

    //删除
    public function delete(){
        $id = I('get.id', 0, 'intval');

        M('ads')->where(['id' => $id])->delete();
        M('ads_detail')->where(['ads_id'=>$id])->delete();

        $this->success('操作成功');
    }
}